package com.jichangxiu.framework.aspect;

import com.jichangxiu.common.annotation.Delete;
import com.jichangxiu.common.annotation.Get;
import com.jichangxiu.common.annotation.Post;
import com.jichangxiu.common.annotation.Put;
import com.jichangxiu.framework.utils.RequestUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Slf4j
@Aspect
@Component
public class RequestAspect {

    @Pointcut("@annotation(post)")
    public void postMappingAround(Post post) {
    }

    @Around(value = "postMappingAround(post)", argNames = "proceedingJoinPoint , post")
    public Object postAround(ProceedingJoinPoint proceedingJoinPoint, Post post) throws Throwable {
        if (post.unifiedResult()) RequestUtils.unifiedResult();
        if (post.removeBlank()) RequestUtils.removeBlank(proceedingJoinPoint);
        return proceedingJoinPoint.proceed();
    }

    @Pointcut("@annotation(delete)")
    public void deleteMappingAround(Delete delete) {
    }

    @Around(value = "deleteMappingAround(delete)", argNames = "proceedingJoinPoint , delete")
    public Object deleteAround(ProceedingJoinPoint proceedingJoinPoint, Delete delete) throws Throwable {
        if (delete.unifiedResult()) RequestUtils.unifiedResult();
        if (delete.removeBlank()) RequestUtils.removeBlank(proceedingJoinPoint);
        return proceedingJoinPoint.proceed();
    }

    @Pointcut("@annotation(put)")
    public void putMappingAround(Put put) {
    }

    @Around(value = "putMappingAround(put)", argNames = "proceedingJoinPoint , put")
    public Object putAround(ProceedingJoinPoint proceedingJoinPoint, Put put) throws Throwable {
        if (put.unifiedResult()) RequestUtils.unifiedResult();
        if (put.removeBlank()) RequestUtils.removeBlank(proceedingJoinPoint);
        return proceedingJoinPoint.proceed();
    }

    @Pointcut("@annotation(get)")
    public void getMappingAround(Get get) {
    }

    @Around(value = "getMappingAround(get)", argNames = "proceedingJoinPoint , get")
    public Object getAround(ProceedingJoinPoint proceedingJoinPoint, Get get) throws Throwable {
        if (get.unifiedResult()) RequestUtils.unifiedResult();
        if (get.removeBlank()) RequestUtils.removeBlank(proceedingJoinPoint);
        return proceedingJoinPoint.proceed();
    }

}
